package com.tangcl.module_base.utils

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.tangcl.module_base.R

/**
 * Created by TCL on 2021/5/18.
 * Describe:字符串工具类
 */
object StringUtils {

    /**
     * 设置前景颜色
     */
    fun setColorText(
        context: Context,
        tv: TextView,
        text: String,
        startIndex: Int,
        endIndex: Int,
        colorId: Int
    ) {
        val sp: Spannable = SpannableString(text)
        sp.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, colorId)),
            startIndex,
            endIndex,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE
        )
        tv.text = sp
    }

    //复制
    fun copyText(
        context: Context?,
        label: String,
        copyText: String
    ) {
        val cbm: ClipboardManager =
            context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData: ClipData = ClipData.newPlainText(label, copyText);
        cbm.setPrimaryClip(clipData)
        Toast.makeText(context, context.getString(R.string.copy_succeed), Toast.LENGTH_SHORT).show()
    }

}